package yz.cp.back.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yz.cp.back.u.entity.UserRole;
import yz.cp.back.u.service.IUserRoleService;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class UserRoleMange {
    public UserRole userRoleConstant = new UserRole();
    @Autowired
    private IUserRoleService userRoleService;

    public void getSimpleAccessUserRoleById(List<String> roleList,Map map) {
        if(roleList.size()==0){
            return;
        }
        List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                .in(UserRole::getId, roleList));
        userRoleList.forEach(v->map.put(v.getId(),v));
    }

    public void getAllUserRoleById(List<String> roleList,Map map) {
        if (roleList.size() == 0) {
            return;
        }
        List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                .in(UserRole::getId, roleList));
        List<String> accessIdList = Lists.newArrayList();
        userRoleList.forEach(v -> {
            map.put(v.getId(), v);
            if (StringUtils.isNotBlank(v.getAccessId())) accessIdList.add(v.getAccessId());
        });
        if(accessIdList.size()>0){
            List<UserRole> userRoleList1 = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                    .in(UserRole::getId, accessIdList));
            userRoleList1.forEach(v -> map.put(v.getId(), v));
        }
    }
    public List getAccessUserRoleById(List<String> roleList) {
        List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                .in(UserRole::getId, roleList));
        List<String> accessIdList = Lists.newArrayList();
        if (userRoleList != null) {
            userRoleList.forEach(v -> accessIdList.add(v.getAccessId()));
        }
        if (accessIdList.size() > 0) {
            Map<String, UserRole> accessMap = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                    .in(UserRole::getId, accessIdList)).stream().collect(Collectors.toMap(UserRole::getId, Function.identity(), (key1, key2) -> key2));
            userRoleList.forEach(v ->
                    v.setAccessName(accessMap.getOrDefault(v.getAccessId(), userRoleConstant).getMerchantAbbrName())
            );
        }
        return userRoleList;
    }


    public List getAccessUserRoleByUserId(List<String> roleList) {
        List<UserRole> userRoleList=userRoleService.list(new QueryWrapper<UserRole>().lambda()
                .in(UserRole::getUserId,roleList));
        List<String> accessIdList=Lists.newArrayList();
        if(userRoleList!=null){
            userRoleList.forEach(v->accessIdList.add(v.getAccessId()));
        }
        if(accessIdList.size()>0){
            Map<String, UserRole> accessMap=userRoleService.list(new QueryWrapper<UserRole>().lambda()
                    .in(UserRole::getId,accessIdList)).stream().collect(Collectors.toMap(UserRole::getId, Function.identity(),(key1, key2)->key2));
            userRoleList.forEach(v->
                    v.setAccessName(accessMap.getOrDefault(v.getAccessId(),userRoleConstant).getMerchantAbbrName())
            );
        }
        return userRoleList;
    }
    public void getAccessUserRole(List<UserRole> userRoleList) {
        if(userRoleList==null||userRoleList.size()==0)
            return;
        List<String> accessIdList = Lists.newArrayList();
        if (userRoleList != null) {
            userRoleList.stream().filter(v -> StringUtils.isNotBlank(v.getAccessId())).forEach(t -> accessIdList.add(t.getAccessId()));
        }
        if (accessIdList.size() > 0) {
            Map<String, UserRole> accessMap = userRoleService.list(new QueryWrapper<UserRole>().lambda()
                    .in(UserRole::getId, accessIdList)).stream()
                    .collect(Collectors.toMap(UserRole::getId, Function.identity(), (key1, key2) -> key2));
            userRoleList.forEach(v -> {
                        v.initNme();
                        v.setAccessName(accessMap.getOrDefault(v.getAccessId(), userRoleConstant).getMerchantAbbrName());
                    }
            );
        }
    }
}
